<?php


namespace app\sapi\controller\v1;


use app\common\controller\Api;
use app\common\model\UserMoneyLog;
use app\admin\model\Shop;
use think\Db;

class Wallet extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 余额明细
     * @DateTime 2023-04-23T15:02:57+0800
     * @return   [type]                   [description]
     */
    public function walletList()
    {
        $page = input('page',1);
        $pageSize = 20;
        $user_id = $this->auth->id;
        $list = Db::name('user_money_log')->where(['user_id'=>$user_id,'log_type'=>['in','1,2,3,4,7,11,10'],'before_freeze_money'=>0,'after_freeze_money'=>0])->order('id DESC')->field('id,log_type,tran_no,money,memo,log_status,create_dt,after_money')->select();
        foreach ($list as $key => &$value) {
            $value['shop_name'] = '';
            $value['log_status'] = '';
            if (in_array($value['log_type'], [2,10,4])) {
                $play = Db::name('play')->where(['play_no'=>$value['tran_no']])->field('shop_name,play_status')->find();
                if ($play) {
                    $value['shop_name'] = $play['shop_name'];
                    $value['log_status'] = $play['play_status']==5?'成功':'进行中';
                }
            } elseif ($value['log_type']==1) {
                $recharge = Db::name('recharge_log')->where(['recharge_no'=>$value['tran_no']])->field('is_pay')->find();
                if ($recharge) {
                    $value['log_status'] = $recharge['is_pay']==1?'成功':'进行中';
                }
            } elseif ($value['log_type']==3) {
                $withdrawal = Db::name('user_withdrawal_log')->where(['id'=>$value['tran_no']])->field('log_status')->find();
                if ($withdrawal) {
                    $value['log_status'] = $withdrawal['log_status']==1?'成功':$withdrawal['log_status']==-1?'失败':'进行中';
                }
            }
        }
        $this->success('余额明细',$list);
    }




    /**
     * 结算明细
     * @DateTime 2023-04-23T15:39:40+0800
     * @return   [type]                   [description]
     */
    public function settlementInfo()
    {
        $page = input('page',1);
        $pageSize = 20;
        $user_id = $this->auth->id;
        $list = Db::name('user_money_log')->where(['user_id'=>$user_id,'log_type'=>['in','10']])->field('id,log_type,tran_no,money,memo,log_status,create_dt,after_money')->order('id DESC')->select();
        foreach ($list as $key => &$value) {
            $value['shop_name'] = '';
            $value['log_status'] = '成功';
            if ($value['log_type']==10) {
                $play_order = Db::name('play_order')->where(['play_order_no'=>$value['tran_no']])->field('shop_name,order_status')->find();
                if ($play_order) {
                    $value['shop_name'] = $play_order['shop_name'];
                }
            }
        }
        $this->success('结算明细',$list);
    }


    public function mydata()
    {
        $date = date('Ymd');
        $where = [];
        $where['start_date'] = ['>=',strtotime($date)];
        $where['end_date'] = ['<=',strtotime($date)+86400];
        $where['play_status'] = ['in',[1,2,3,4,5]];
        $where['user_id'] = $this->auth->id;
        // 今日订单数
        $jrdds = (int)\app\admin\model\Play::where($where)->value('(sum(num)-sum(stock)) as jrdds');
        // 今日消费金额
        $where = [];
        $where['p.start_date'] = ['>=',strtotime($date)];
        $where['p.end_date'] = ['<=',strtotime($date)+86400];
        $where['po.order_status'] = ['in',[3,-7,-8,-9]];
        $where['p.user_id'] = $this->auth->id;
        $jrxfje = (float)Db::name('play')->alias('p')->join('play_order po','po.play_no=p.play_no')->where($where)->value('sum(po.service_charge_amt+po.actual_rebate_amt)');
        $this->success('',[
            'jrdds' => $jrdds,
            'jrxfje' => $jrxfje
        ]);
    }


    /**
     * 外卖运营计算器
     * @DateTime 2023-04-27T22:39:47+0800
     * @param    string                   $value [description]
     */
    public function calculator()
    {
        $one = input('one/d',0);
        $two = input('two/d',0);
        $three = input('three/d',0);
        $four = input('four/d',0);
        $five = input('five/d',0);
        $add = input('add/d',0);
        // if (!$one || !$two || !$three || !$four || !$five || !$add) {
        //     $this->error('缺少参数');
        // }
        // $user_id = $this->auth->id;
        // $end_time = strtotime(date('Ymd'));
        // $start_time = $end_time-30*86400;
        // $where = [];
        // $where['po.place_time'] = ['<',$end_time];
        // $where['po.place_time'] = ['>=',$start_time];
        // $where['pop.merchant_id'] = $user_id;
        // $where['pop.score'] = ['>',0];
        // $list = Db::name('play_order')->alias('po')->join('chbc_play_order_proof pop','pop.play_order_no=po.play_order_no')->where($where)->field('pop.score')->select();
        // if (count($list)<5) {
        //     $this->success('不展示评分',[
        //         'bdypf' => '',
        //         'tshpf' => '',
        //     ]);
        // } else {
            $bdypf = round(($one*1+$two*2+$three*3+$four*4+$five*5)/($one+$two+$three+$four+$five),1);
            $tshpf = round(($one*1+$two*2+$three*3+$four*4+$five*5+$add*5)/($one+$two+$three+$four+$five+$add),1);
            $this->success('评分',[
                'bdypf' => $bdypf,
                'tshpf' => $tshpf,
            ]);
        // }
    }
}